const date = new Date()
const month = date.getMonth() + 1
const day = date.getDate()

// 时间
function times() {
    var today = new Date();
    var time = document.querySelector('.time');
    time.innerHTML = '';
    time.innerHTML += (today.getFullYear() + '.');
    time.innerHTML += (today.getMonth() + 1 + '.');
    time.innerHTML += (today.getDate() + '<br>');

    var dateArr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var day = today.getDay();
    time.innerHTML += (dateArr[day]);
}
times();

// 本地数据的取出
const uname = JSON.parse(localStorage.getItem('nowLogin'))
let arr = JSON.parse(localStorage.getItem(`${uname}+${month}+${day}`))
let arrTom = JSON.parse(localStorage.getItem(`${uname}+${month}+${day+1}`))

// 今天、明天的转换
const plan = document.querySelector('.plan_left')
const selects = plan.querySelectorAll('[data-id]')
selects.forEach(function(item) {
    item.addEventListener('click', function(e) {
        selects.forEach(function(item) {
            item.removeAttribute('id')
        })
        e.target.setAttribute('id', 'active')
    })
})


// console.log(localStorage.key(arr))
// 获取ul
const ul = document.querySelector('.swiper-slide')

function render() {
    let trArr = arr.map(function(item, i) {
        return `<li>
                ${item}
                <span class="throght"></span>
                <span class="del"></span>
                </li>
        `
    })
    ul.innerHTML = trArr.join('');
}
render();


// 事件委托
ul.addEventListener('click', function(e) {
    const spanBig = ul.querySelectorAll('span')
    spanBig.forEach(function(item) {
        item.style.opacity = 0
    })
    if (e.target.tagName = 'LI') {
        const spans = e.target.querySelectorAll('span')
        spans.forEach(function(item) {
            item.style.opacity = 1
        })
    }
})

// 删除li
const del = ul.querySelectorAll('.del')
del.forEach(function(item, index) {
    item.addEventListener('click', function(e) {
        console.log(arr);
        arr.splice(index, 1)
        console.log(arr);
        arr = localStorage.setItem(`${uname}+${month}+${day}`, JSON.stringify(arr))
        arr = JSON.parse(localStorage.getItem(`${uname}+${month}+${day}`))
        console.log(arr);
        render();
    })
})



// 添加删除线
const throght = ul.querySelectorAll('.throght')
let flag = true
let lineArr = []
throght.forEach(function(item) {
        item.addEventListener('click', function(e) {
            if (flag) {
                this.parentNode.style.textDecoration = 'line-through'
                lineArr.push(this.parentNode)
                console.log(lineArr);
                localStorage.setItem('lineArr', JSON.stringify(lineArr.length))
                flag = false
            } else {
                this.parentNode.style.textDecoration = 'none'
                flag = true
            }
        })
    })
    // console.log(lineArr.length);
selects[0].addEventListener('click', function() {
    window.location.reload()
})

selects[1].addEventListener('click', function() {
    function render() {
        let trArr = arrTom.map(function(item, i) {
            return `<li>
                    ${item}
                    <span class="throght"></span>
                    <span class="del"></span>
                    </li>
            `
        })
        ul.innerHTML = trArr.join('');
    }
    render();
    const spanBig = ul.querySelectorAll('span')
    spanBig.forEach(function(item) {
        item.style.opacity = 0
    })

    // 事件委托
    ul.addEventListener('click', function(e) {
        const spanBig = ul.querySelectorAll('span')
        spanBig.forEach(function(item) {
            item.style.opacity = 0
        })
        if (e.target.tagName = 'LI') {
            const spans = e.target.querySelectorAll('span')
            spans.forEach(function(item) {
                item.style.opacity = 1
            })
        }
    })

    // 删除li
    const del = ul.querySelectorAll('span:nth-child(even)')
    del.forEach(function(item, index) {
        item.addEventListener('click', function(e) {
            console.log(arrTom);
            arrTom.splice(index, 1)
            console.log(arrTom);
            arrTom = localStorage.setItem(`${uname}+${month}+${day}`, JSON.stringify(arrTom))
            arrTom = JSON.parse(localStorage.getItem(`${uname}+${month}+${day}`))
            console.log(arrTom);
            render();
        })
    })

    // 添加删除线
    const throght = ul.querySelectorAll('span:nth-child(odd)')
    let flag = true
    throght.forEach(function(item) {
        item.addEventListener('click', function(e) {
            if (flag) {
                this.parentNode.style.textDecoration = 'line-through'
                flag = false
            } else {
                this.parentNode.style.textDecoration = 'none'
                flag = true
            }
        })
    })
})